//
//  XTEmoticonCollectionViewCell.swift
//  Weibo(Swift)
//
//  Created by 肖霆 on 16/5/22.
//  Copyright © 2016年 肖霆. All rights reserved.
//

import UIKit

class XTEmoticonCollectionViewCell: UICollectionViewCell {
    //删除按钮
    private lazy var deleteEmoticonBtn:UIButton = {
        let btn = UIButton()
        btn.addTarget(self, action: "didSelectedDelteEmoticonBtn", forControlEvents: .TouchUpInside)
        btn.setImage(UIImage(named:"compose_emotion_delete" ), forState: .Normal)
        btn.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: .Highlighted)
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    //通过表情数组给表情按钮设置表情图片
    var emoticonArray:[XTEmoticon]?{
        didSet{
            //把表情数组里的表情拿出来，把button数组中的btn拿出来，对应放在上面
            guard let ets = emoticonArray else{
                return
            }
            
            //避免重用问题   此处如果不隐藏，那么空的没有赋值的表情位置则会被前一页的表情填充
            for btn in emoticonBtnArray{
                btn.hidden = true
            }
            //遍历表情数组给表情按钮赋值
            for (i,emoticon) in ets.enumerate(){
                let btn = emoticonBtnArray[i]
                btn.emoticon = emoticon
            }
        }
    
    
    }
    
    //保存表情按钮的数组
    private lazy var emoticonBtnArray:[XTEmoticonBtn] = [XTEmoticonBtn]()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        addEmoticonBtn()
        contentView.addSubview(deleteEmoticonBtn)
    
    }

    //添加表情控件的方法
    private func addEmoticonBtn(){
        
        for _ in 0..<20{
            let btn = XTEmoticonBtn()
            btn.addTarget(self, action: "emoticonBtnClick:", forControlEvents: .TouchUpInside)
//            btn.backgroundColor = RandomColor()
            btn.titleLabel?.font = UIFont.systemFontOfSize(34)
            contentView.addSubview(btn)
            emoticonBtnArray.append(btn)
        }
    }
    
    override func layoutSubviews() {
        let itemWidth:CGFloat = width/7
        let itemHeight:CGFloat = height/3
        //enumerate() 指一个（i，x）对，其中i为数组中的序号，x是这个元素本身的值
        for (i,btn) in emoticonBtnArray.enumerate(){
         
            btn.size = CGSize(width: itemWidth, height: itemHeight)
            let col = i % 7
            let row = i / 7
            
            btn.x = itemWidth * CGFloat(col)
            btn.y = itemHeight * CGFloat(row)
        }
        
        //设置删除按钮的位置
        deleteEmoticonBtn.size = CGSizeMake(itemWidth, itemHeight)
        deleteEmoticonBtn.x = width - itemWidth
        deleteEmoticonBtn.y = height - itemHeight
    }
        //  MARK: -点击删除表情按钮，发送通知-
    @objc private func emoticonBtnClick(btn:XTEmoticonBtn){
        let emoticon = btn.emoticon
        NSNotificationCenter.defaultCenter().postNotificationName(didSelectedEmoticonBtnNotification, object: emoticon)
    
    }
    
    @objc private func didSelectedDelteEmoticonBtn(){
        NSNotificationCenter.defaultCenter().postNotificationName(didSelectedDeleteEmoticonBtnNotification, object: nil)
    
    }
}
